package com.example.carrent.controller;


import com.example.carrent.bean.UserOrders;
import com.example.carrent.service.UserOrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@CrossOrigin
@RestController
@RequestMapping("alipay")
public class notifyController {

    @Autowired
    private UserOrdersService userOrdersService;
    @PostMapping("/notify")
    public String notify(String out_trade_no,String body){
        System.out.println("支付宝回调进入");
        System.out.println(out_trade_no+"测试outTradeNo"+body);
        Map<String, Object> map =getidandcoresids(body);

        UserOrders userOrders=new UserOrders();
        userOrders.setOrderNumber(out_trade_no);
        userOrders.setPaymentStatus("已完成");
        userOrders.setUserId(Integer.valueOf((String) map.get("userid")));
        userOrders.setOrderIds((String) map.get("ids"));
        userOrders.setCourseIds((String) map.get("courseids"));
        System.out.println(".......................................................................");
        System.out.println(userOrders+"userOrders");
        // userId  outTradeno
        String str = "";

        String[] courseIdStrs = userOrders.getCourseIds().split(",");
        Integer[] courseIds = new Integer[courseIdStrs.length];
        //拆分ids集合
        String[] orderIds = userOrders.getOrderIds().split(",");
        Integer[] orderIdsInt = new Integer[orderIds.length];
        for (int i = 0; i < courseIdStrs.length; i++) {
            courseIds[i] = Integer.valueOf(courseIdStrs[i].trim());
            orderIdsInt[i] = Integer.valueOf(orderIds[i].trim());

            userOrders.setCourseId(courseIds[i]);
            userOrders.setOrderId(orderIdsInt[i]);
            userOrdersService.payOrder(userOrders);
        }
        return "success"; // 必须返回"success"字符串，支付宝才会认为通知成功
    }
    // ids=142,143courseids=3,4
    public static Map<String, Object> getidandcoresids(String input){
        int idsIndex = input.indexOf("ids=");
        if (idsIndex == -1) throw new IllegalArgumentException("缺少 ids 参数");
        int courseidsIndex = input.indexOf("courseids=", idsIndex + 4);
        if (courseidsIndex == -1) throw new IllegalArgumentException("缺少 courseids 参数");
        String idsValue = input.substring(idsIndex + 4, courseidsIndex);

        // 提取 courseids 后的值
        int useridIndex = input.indexOf("userid=", courseidsIndex + 10);
        if (useridIndex == -1) throw new IllegalArgumentException("缺少 userid 参数");
        String courseidsValue = input.substring(courseidsIndex + 10, useridIndex);

        // 提取 userid 后的值
        String useridValue = input.substring(useridIndex + 7);
        Map<String, Object> map = new HashMap<>();
        map.put("ids",idsValue);
        map.put("courseids",courseidsValue);
        map.put("userid",useridValue);
        System.out.println("ids 后的值: " + idsValue);
        System.out.println("courseids 后的值: " + courseidsValue);
        System.out.println("userid 后的值: " + useridValue);
        return  map;
    }
}
